<template>
	<view class="container">
		<view class="line"></view>
		<view class="addaddress-form">
		      <view class="form-item">
		      	<text class="form-item-title">真实姓名</text>
				<input placeholder="请填写姓名" v-model="username"/>
		      </view>
			  <view class="form-item">
			  	<text class="form-item-title">手机号码</text>
				<input placeholder="请填写手机号码" v-model="userphone"/>
			  </view>
			  <view class="form-item" @tap="openSelectarea()">
			  	<text class="form-item-title">所在地区</text>
				<text class="area-text-place area-text" v-if="userarea==''">请选择所在地区</text>
				<text class="area-text" v-else>{{userarea}}</text>
				<u-icon name="/static/icon_zy_189.png" size="15"></u-icon>
			  </view>
			  <view class="form-item">
			  	<text class="form-item-title">详细地址</text>
				<textarea placeholder="街道、楼牌号等" v-model="useradd" auto-height="true"></textarea>
			  </view>
		</view>
		<text class="deleteadd" @tap="openAskdeladdModal()" v-if="iseditOradd=='edit'">删除地址</text>
		<view class="submit-btn" @tap="submitFucselect()">
			<text>保存</text>
		</view>
		<u-modal content="确定删除该地址？" confirm-color="rgba(14, 174, 100, 1)" :show-cancel-button="true" v-model="showAskDeletemodal" @confirm="requestDeladd()"></u-modal>
		<city-select v-model="showSelectadd" @city-change="cityChange"></city-select>
	</view>
</template>

<script>
	import {addHomeaddress,updateHomeAddress,deleteAdd} from '@/apis/request.js';
	import citySelect from './u-city-select.vue';
	export default {
		components: {
			citySelect
		},
		data() {
			return {
				showAskDeletemodal:false,
				iseditOradd:'',
				username:'',
				userphone:'',
				userarea:'',
				useradd:'',
				usertoken:'',
				showSelectadd:false,
				addid:''
			}
		},
		created() {
			this.usertoken = uni.getStorageSync('userInfo').token;
		},
		onLoad(options) {
			this.iseditOradd=options.mode;
			if(options.mode=='add'){
			    uni.setNavigationBarTitle({
			    	title:'新建地址'
			    });
			}else if(options.mode=='edit'){
				uni.setNavigationBarTitle({
					title:'编辑地址'
				});
				this.username=uni.getStorageSync('eidtaddInfo').name;
				this.userphone=uni.getStorageSync('eidtaddInfo').phone;
				this.userarea=uni.getStorageSync('eidtaddInfo').area;
				this.useradd=uni.getStorageSync('eidtaddInfo').address;
				this.addid=uni.getStorageSync('eidtaddInfo').id;
			}
		},
		onUnload() {
			if(this.iseditOradd=='edit'){
				uni.removeStorageSync('eidtaddInfo');
			}
		},
		methods: {
			requestAddaddress(){
				if(this.username==''){
				    uni.showToast({
						icon:'none',
				    	title:'请填写姓名'
				    });
					return ;
				}
				if(this.userphone == ''){
					uni.showToast({
						icon:'none',
						title:'请填写手机号'
					});
					return ;
				}
				if(this.userphone.length != 11 && isNaN(this.userphone)){
					uni.showToast({
						icon:'none',
						title:'请正确输入手机号'
					});
					return ;
				}
				if(this.userarea == ''){
					uni.showToast({
						icon:'none',
						title:'请选择所在地区'
					});
					return ;
				}
				if(this.useradd == ''){
					uni.showToast({
						icon:'none',
						title:'请输入详情地址'
					});
					return ;
				}
				this.$http({
					url:addHomeaddress,
					method:"POST",
					data:{
						address:this.useradd,
						name:this.username,
						phone:this.userphone,
						area:this.userarea
					},
					header:{
						'content-type':'application/x-www-form-urlencoded',
						'Token':this.usertoken
					}
				})
				.then(res=>{
					// console.log(res);
					if(res[1].data.status=='200'){
						uni.showToast({
							title:'添加成功'
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							});
						},200);
					}
				})
				.catch(err=>{
					console.log(err);
				});
			},
			requestUpdateAddress(){
				if(this.username==''){
				    uni.showToast({
						icon:'none',
				    	title:'请填写姓名'
				    });
					return ;
				}
				if(this.userphone == ''){
					uni.showToast({
						icon:'none',
						title:'请填写手机号'
					});
					return ;
				}
				if(this.userphone.length != 11 && isNaN(this.userphone)){
					uni.showToast({
						icon:'none',
						title:'请正确输入手机号'
					});
					return ;
				}
				if(this.userarea == ''){
					uni.showToast({
						icon:'none',
						title:'请选择所在地区'
					});
					return ;
				}
				if(this.useradd == ''){
					uni.showToast({
						icon:'none',
						title:'请输入详情地址'
					});
					return ;
				}
				this.$http({
					url:updateHomeAddress,
					data:{
						id:this.addid,
						address:this.useradd,
						area:this.userarea,
						name:this.username,
						phone:this.userphone
					},
					method:"POST",
					header:{
						'content-type':'application/x-www-form-urlencoded',
						'Token':this.usertoken
					}
				})
				.then(res=>{
					// console.log(res);
					if(res[1].data.status=='200'){
						uni.showToast({
							title:'修改成功'
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							});
						},200);
					}
				})
				.catch(err=>{
					console.log(err);
				});
			},
			submitFucselect(){
				if(this.iseditOradd=='add'){
					this.requestAddaddress();
				}else if(this.iseditOradd=='edit'){
					this.requestUpdateAddress();
				}
			},
			openAskdeladdModal(){
				this.showAskDeletemodal=true;
			},
			cityChange(e){
				this.userarea = `${e.province.label}${e.city.label}${e.area.label}`;
			},
			openSelectarea(){
				this.showSelectadd=true;
			},
			requestDeladd(){
				this.$http({
					url:deleteAdd,
					data:{
						id:this.addid
					},
					method:"POST",
					header:{
						'content-type':'application/x-www-form-urlencoded',
						'Token':this.usertoken
					}
				})
				.then(res=>{
					// console.log(res);
					if(res[1].data.status=='200'){
						uni.showToast({
							title:'删除成功'
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							});
						},200);
					}
				})
				.catch(err=>{
					console.log(err);
				});
			}
		}
	}
</script>

<style lang="scss">
    page{
		width: 100%;
		height: 100%;
	}
	.container{
		width: 100%;
		height: 100%;
		.line{width: 100%;height:20rpx;background-color: rgba(244, 248, 251, 1);}
		.addaddress-form{
			width: 690rpx;
			height: auto;
			margin: auto;
			.form-item{
				width: 100%;
				
				padding-top: 30rpx;
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #F1F1F1;
				display: flex;
				align-items: center;
				.form-item-title{font-size: 28rpx;}
				input{font-size: 28rpx;flex: 1;margin-left: 20rpx;}
				textarea{font-size: 28rpx;flex: 1;margin-left: 20rpx;}
				.area-text{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size: 28rpx;flex: 1;margin-left: 20rpx;}
				.area-text-place{color: #888;}
			}
		}
		.deleteadd{color: rgba(14, 174, 100, 1);font-size: 28rpx;margin-top: 30rpx;margin-left: 30rpx;display: inline-block;}
		.submit-btn{width: 690rpx;height: 90rpx;background-color: rgba(14, 174, 100, 1);color: #FFFFFF;display: flex;justify-content: center;align-items: center;font-size: 28rpx;border-radius: 10rpx;position: fixed;bottom:50rpx;left: 30rpx;}
	}
</style>
